<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_xiaofeelian/model')->方法名();
*/
namespace jy_xiaofeelian;
defined('BY_JYA') or exit('error');
class city extends \plugin{
	public function gocity($rq=array()){
		$basic=$this->plugin_setting('basic');
		if($basic['is_onecity']){
			$cityid=$this->uponecity($basic);
			$city=array(
				'id'=>$cityid,
				'pcd'=>$_SESSION['pcd'],
				'isone'=>1,
			);
			return $city;
		}
		if($rq['cityid']){
			$_SESSION['cityid']=intval($rq['cityid']);
		}
		if($_SESSION['cityid']){
			$city=pdo_get('jy_xiaofeelian_city',array('id'=>$_SESSION['cityid'],'uniacid'=>$_SESSION['uniacid']));
			$city['pcd']=json_decode($city['pcd'],true);
			$_SESSION['pcd']=$city['pcd'];
			$_SESSION['pcd']['title']=$city['pcd']['city'];
		}
		if(!$city){
			$pcd=$_SESSION['pcd']?:array('province'=>'广东省','city'=>'广州市');
			$cityid=$this->upcity('广州市',$pcd);
			$city=pdo_get('jy_xiaofeelian_city',array('id'=>$cityid,'uniacid'=>$_SESSION['uniacid']));
			$city['pcd']=json_decode($city['pcd'],true);
		}
		$city['pcd']['title']=$city['title'];
		return $city;
	}
	public function upcity($city='',$pcd=array()){
		$basic=$this->plugin_setting('basic');
		if($basic['is_onecity']){
			$cityid=$this->uponecity($basic);
			return $cityid;
		}
		$_SESSION['pcd']=$pcd;
		$_SESSION['pcd']['title']=$city;
		$cityinfo = pdo_get('jy_xiaofeelian_city',array('title'=>$city,'uniacid'=>$_SESSION['uniacid']));
		$cityid=$cityinfo['id'];
		if(!$cityid){
			$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'title'=>$city,
					'pcd'=>ijson_encode($pcd),
					'status'=>1,
					'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_xiaofeelian_city',$insert);
			$cityid=pdo_insertid();
		}
		$_SESSION['cityid']=$cityid;
		return $cityid;
	}
	public function uponecity($basic){
		$_SESSION['pcd']=$basic['onepcd'];
		$title=$basic['onetype']?$basic['onepcd']['district']:$basic['onepcd']['city'];
		$_SESSION['pcd']['title']=$title;
		$cityinfo = pdo_get('jy_xiaofeelian_city',array('isone'=>1,'title'=>$title,'uniacid'=>$_SESSION['uniacid']));
		$cityid=$cityinfo['id'];
		if(!$cityid){
			$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'title'=>$title,
					'pcd'=>ijson_encode($basic['onepcd']),
					'status'=>1,
					'isone'=>1,
					'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_xiaofeelian_city',$insert);
			$cityid=pdo_insertid();
		}
		$_SESSION['cityid']=$cityid;
		return $cityid;
	}
}
?>